window.K=window.K||{"URL":{"defaultTemplates":""}};
window._jstemplates=window._jstemplates||{};
K=$.extend(K,{
	"LeftMenu":{
		"View":null,
		"template":{"name":"default","url":"template.html"},
		"installation":function(){
			K.LeftMenu.View=Backbone.View.extend({
			    "tagName":"div",
			    "className":"el-wrapper",
			    "template":_.template($("#_template>#t_leftmenu").text()),
			    "dataURL":false,
			    "data":false,
			    "events":{"onclick":function(){}},
			    "wrapper":null,
			    "setData":function(data_url){
			    	if(typeof(data_url)=="string")this.dataURL=data_url;
			    	else{this.data=data_url;}
			    	return this;
			    },"setURL":function(url){
			    	this.dataURL=url;
			    	return this;
			    },
			    "to":function($dom,showArrow){
			    	this.wrapper=$dom;
			    	this.$el.empty();
			    	this.events=$.extend({},this.events);
			    	if(this.data){
			    		this.render(this.data);
			    		$dom.append(this.$el);
			    		this.activate(showArrow);
			    	}else{
			    		$.ajax({"url":this.dataURL||K.URL.leftMenu,"dataType":"json","data":{"_":$.now()},
			    			    "dom":$dom,"_this":this}).done(function(json){
							this._this.render(json);
							this.dom.append(this._this.$el);
							this._this.activate(showArrow);
						}).fail(function(e1,e2,e3){
							K.Tool.log(e2);
						});
			    	}
			    	return this;
			    },"activate":function(showArrow){
			    	var menu=this,$menu=this.$el;
			    	$menu.off("click");
			    	$menu.on("click",function(event){event.stopPropagation();});
			    	$menu.find(".menu-lv1>li").each(function(){
			    		$(this).off("click");
						$(this).on("click",function(){
							$(this).siblings().removeClass("active");
							$(this).addClass("active");
						});
						$(this).unbind("hover");
						$(this).hover(function(event){
							if($(this).siblings(".active").length>0){
								$(this).siblings(".active").removeClass("active");
							}
						},function(){});
					});
					$menu.find(".menu-lv2>li:not('.leaf')").each(function(){
						$(this).off("click");
						$(this).on("click",function(){
							$(this).toggleClass("active");
							if($(this).hasClass("active")){
								$(this).siblings().removeClass("active");
							}
						});
					});
					$menu.find(".menu-lv2").css("left",this.wrapper.width()+"px");
					if(typeof(showArrow)=="string"&&showArrow.length>1&&showArrow.charAt(0)=="#"){
						$menu.find(".itemv1-arrow").each(function(){$(this).css("background-color",showArrow);});
					}
					$menu.find(".menu-lv2>.leaf,.menu-lv3>.leaf").each(function(){$(this).off("click");});
					$menu.find(".leaf").each(function(){
						$(this).on("click",function(menu,$leaf,showArrow){
							return function(event){
								event.stopPropagation();
								menu.$el.find(".leaf").removeClass("active");
								$leaf.addClass("active");
								var $li1=$leaf.parent().hasClass("menu-lv1")?$leaf:menu.$el.find(".menu-lv1").children("li:has('.leaf.active')");
								if(!$li1.hasClass("static")){$li1.addClass("static");}
								if(!$li1.hasClass("active")){$li1.addClass("active");}
								$li1.siblings().removeClass("static").removeClass("active");
								if(showArrow){
									menu.$el.find(".itemv1-arrow").hide();
									menu.$el.find(".static>.itemv1-arrow").show();
								}
								var arr=[],
							  	  getTxt=function($dom){
								  	arr.push($dom.attr("data-id")+"→"+$.trim($dom.children("nobr").text()));
								  	if(!$dom.parent().hasClass("menu-lv1")){
								  		$dom=$dom.parent().closest("li");
								  		getTxt($dom);
								  	}else{
								  		$dom=$dom.parent().closest("li");
								  		arr.push($dom.attr("data-id")+"→"+$.trim($dom.children("nobr").text()));
								  	}
					  	    	 };
							  getTxt($leaf);
							  arr.reverse();
							  if(arr.length>1)arr.shift();
								menu.events.onclick($leaf,arr);
							};
						}(menu,$(this),showArrow));
					});
					$("body").on("click",function(menu){
						return function(){
							menu.$el.find(".menu-lv1>.active").removeClass("active");
						};
					}(menu));
			    },"click":function(fn){
			    	if(typeof(fn)=="function"){this.events.onclick=fn;}
			    	return this;
			    },
			    "render":function(json){  
			        this.$el.html(this.template(json));
			        return this.$el;
			     }
			});
		},
		"init":function($dom,color){
			$dom.empty();
			var menu=new K.LeftMenu.View();
			if(!K.URL.leftMenu){
				menu.setData("../../json/emp/leftmenu.json");
			}
			menu.to($dom,color);
			return menu;
		}
	}
});
$.extend(_jstemplates,{"LeftMenu":function(cb){
	if($("#_template").length<=0){
		$("body").append($("<div type='text/template' id='_template' />"));
	}
	var arr=[];
	arr.push('<script id="t_leftmenu" type="text/template">');
	arr.push('<ul class="menu-lv1">');
	arr.push('{for(var i=0;i<data.length;i++){var item=data[i];}');
	arr.push('<li data-id="{-item.id}" {if(!item.children){}class="leaf"{}}><div class="icon {-item.icon}"></div><nobr>{-item.text}</nobr>');
	arr.push('<div class="itemv1-arrow"></div>');
	arr.push('{if(typeof(item.children)=="object"){}');
	arr.push('<ul class="menu-lv2 bg-c4">');
	arr.push('{for(var j=0;j<item.children.length;j++){var itemv2=item.children[j];}');
	arr.push('<li data-id="{-itemv2.id}" {if(!itemv2.children){}class="leaf"{}}><nobr>{-itemv2.text}</nobr>');
	arr.push('{if(typeof(itemv2.children)=="object"){}');
	arr.push('<span class="arrow"></span>');
	arr.push('<ul class="menu-lv3 bg-c4">');
	arr.push('{for(var k=0;k<itemv2.children.length;k++){var itemv3=itemv2.children[k];}');
	arr.push('<li data-id="{-itemv3.id}" title="{-itemv3.text}" class="leaf">');
	arr.push('<nobr>{-itemv3.text}</nobr>');
	arr.push('</li>');
	arr.push('{}}');
	arr.push('</ul>');
	arr.push('{}}');
	arr.push('</li>');
	arr.push('{}}');
	arr.push('</ul>');
	arr.push('{}}');
	arr.push('</li>');
	arr.push('{}}');
	arr.push('</ul>');
	arr.push('</script>');
	if($("#_template").find("#t_leftmenu").length<=0)
	$("#_template").append(arr.join(""));
	if(typeof(cb)=="function")cb();
}});